package controller.tongji;

import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

public class UIcash extends JDialog implements ActionListener{
    JPanel p1,p2;
    JLabel count;
    JComboBox jComboBox;    //下拉框

    JTable table;
    String[][] obj;//表格数据
    JScrollPane scrollpane; //表格面板
    String arr[];

    ResultSet resultSet;
    JdbcDeal jdbcDeal;
    String sql;
    int Count;        //获取信息行数

    public void UpdateTable(){
        p2.remove(scrollpane);
        table=new JTable(obj,arr);
        table.setRowHeight(30);
        scrollpane=new JScrollPane(table);
        p2.add(scrollpane, BorderLayout.CENTER);
        p2.revalidate();
        this.revalidate();
    }

    private boolean Table_select(String[] arr){
        try {
            if(arr.length==3){
                 sql = "SELECT * FROM v_sale_numandcash order by cash desc";
            }else if(arr.length==2){
                sql = "SELECT * FROM v_worker_cash order by cash desc";
            }
            jdbcDeal = new JdbcDeal();
            resultSet = jdbcDeal.select(sql);
            ResultSetMetaData rsmd = resultSet.getMetaData();        // 返回元数据对象
            resultSet.last();
            Count = resultSet.getRow();
            resultSet.beforeFirst();
            obj = new String[Count][arr.length];
            int i=0;
            if(!resultSet.next()){
                jdbcDeal.close();
                JOptionPane.showMessageDialog(this, "未查询到相关信息", "", JOptionPane.WARNING_MESSAGE);
                return false;
            }else{
                do{
                    obj[i][0] = resultSet.getString("name");
                    if(arr.length==3){
                        obj[i][1] = resultSet.getString("salenum");
                        obj[i][2] = resultSet.getString("cash");
                    }else if(arr.length==2){
                        obj[i][1] = resultSet.getString("cash");
                    }
                    i++;
                }while (resultSet.next());
                jdbcDeal.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return true;
    }

    public UIcash() {
        this.setTitle("消费统计");
        this.setSize(600,450);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setModal(true);    //模态窗口

        p1 = new JPanel();
        jComboBox = new JComboBox(new String[]{"按商品","按销售人员"});
        p1.add(jComboBox);

        arr = new String[]{"货物","销售总数","总销售额"};
        Table_select(arr);


        table=new JTable(obj,arr);
        //表格限宽
        table.setRowHeight(30);
        //创建显示表格的滚动面板
        scrollpane=new JScrollPane(table);
        p2 = new JPanel();
        p2.setLayout(new BorderLayout());
        p2.add(scrollpane,BorderLayout.CENTER);

        Box boxV = Box.createVerticalBox();
        boxV.add(p1);
        boxV.add(p2);
        this.add(boxV,BorderLayout.CENTER);

        jComboBox.addActionListener(this);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        boolean select_flag = false;
        String jBox = jComboBox.getSelectedItem().toString();
        if(jBox.equals("按销售人员")){
            arr = new String[]{"货物","总销售额"};
            select_flag = Table_select(arr);
        }else if(jBox.equals("按商品")){
            arr = new String[]{"货物","销售总数","总销售额"};
            select_flag = Table_select(arr);
        }
        if(select_flag){
            p2.remove(scrollpane);
            UpdateTable();
            return;
        }
    }
}
